package com.sx.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.cache.redis.UrlDataCash;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.PropertiesUtil;
import com.dfec.utils.ResponseUtil;
import com.sx.service.FarmActivityService;

/**
 * Servlet implementation class ForcastController
 */
@WebServlet(name = "fa", description = "二十四节气与农事活动,farmactivity", urlPatterns = { "/fa" })
public class FarmActivityController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	FarmActivityService s=new FarmActivityService();
	private List<Object> list=null;
    Msg msg=null;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		msg=new Msg();
		list=new ArrayList<>();
		
		String type = request.getParameter("type");
		try {
			if (type.equals("list")) {
			
				String page=request.getParameter("page");
				String size=request.getParameter("size");
				String keyWord =request.getParameter("keyword");
				
				List<Object> getdata=s.list(page, size,keyWord);
				if(!getdata.get(0).toString().equals("[]")){
					msg.setCode(Consists.DATA_Code_OK);
					msg.setMsg(Consists.DATA_Msg_OK);
				}else {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
				}
				list.add(msg);
				list.add(getdata.get(0));
				//设置缓存
				if(msg.getCode().equals(Consists.DATA_Code_OK)&&PropertiesUtil.config_redis_use) {
					UrlDataCash.saveUrlData(UrlDataCash.urlKey(request).toString(), JSON.toJSONString(list));
				}
				 ResponseUtil.sendScript(response, JSON.toJSONString(list));
			}else if(type.equals("diteals")) {
					String id=request.getParameter("id");
						List<Object> getdata=s.getDiteals(id);
						if(!getdata.get(0).toString().equals("[]")){
							msg.setCode(Consists.DATA_Code_OK);
							msg.setMsg(Consists.DATA_Msg_OK);
						}else {
							msg.setCode(Consists.DATA_Code_OK__NODATA);
							msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
						}
						list.add(msg);
						list.add(getdata.get(0));
						//设置缓存
						if(msg.getCode().equals(Consists.DATA_Code_OK)&&PropertiesUtil.config_redis_use) {
							UrlDataCash.saveUrlData(UrlDataCash.urlKey(request).toString(), JSON.toJSONString(list));
						}
						 ResponseUtil.sendScript(response, JSON.toJSONString(list));
			}else if(type.equals("one")) {
		
					List<Object> getdata=s.getOne();
					if(!getdata.get(0).toString().equals("[]")){
						msg.setCode(Consists.DATA_Code_OK);
						msg.setMsg(Consists.DATA_Msg_OK);
					}else {
						msg.setCode(Consists.DATA_Code_OK__NODATA);
						msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
					}
					list.add(msg);
					list.add(getdata.get(0));
					//设置缓存
					if(msg.getCode().equals(Consists.DATA_Code_OK)&&PropertiesUtil.config_redis_use) {
						UrlDataCash.saveUrlData(UrlDataCash.urlKey(request).toString(), JSON.toJSONString(list));
					}
					 ResponseUtil.sendScript(response, JSON.toJSONString(list));
				}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list.add(msg);
			list.add(new ArrayList<>());
			ResponseUtil.sendScript(response, JSON.toJSONString(list));
		}
		
		
	}
}